﻿using System;
using System.IO;
using ProjScan.Config;
using ProjScan.RecordManager;
using ProjScan.ScanGUI;
using ProjScan.ScanRule;
using ProjScan.ScanRule.Attr;
using ProjScan.Submodules.CS_Check;
using ProjScan.Tools;

namespace ProjScan.Modules
{
	// Token: 0x02000305 RID: 773
	internal class CSCheckModule : BaseModules<CSCheckModule, CSCheckConfig>
	{
		// Token: 0x06001248 RID: 4680 RVA: 0x0005511C File Offset: 0x0005331C
		public override void Run(StreamWriter ruleStreamWriter, StreamWriter thresholdStreamWriter)
		{
			Singleton<PerformanceMeasurer>.Instance.PushState("CS_Check");
			ScanLog.Instance.LogInfo(Module.CS_Check);
			ToolsHelper.ShowProgressBar("CS Check", "Initializing...", 0.01f, false);
			this.rulesSw = ruleStreamWriter;
			this.thresSw = thresholdStreamWriter;
			this.Init();
			this.LoadRules(LoadMode.Check);
			bool flag = this.baseRules_Check == null || this.baseRules_Check.Count == 0;
			if (flag)
			{
				ScanLog.Instance.LogInfo(Module.Basic_Assets_Check, ScanLog.Stage.s2, "Rules is empty");
				ScanLog.UnityLogError("CS脚本检测运行失败：相关规则全部关闭");
				this.Close();
			}
			else
			{
				this.InitRules(3000);
				RecordManagerFactory.Instance.RecordRuleInit(this.recordRules_Check);
				ScanLog.Instance.LogInfo(Module.CS_Check, ScanLog.Stage.s2, "Dispatching rules done");
				try
				{
					ToolsHelper.ShowProgressBar("CS Check", "Scanning...", 0.3f, false);
					RecordManagerFactory.Instance.PrepareAssets();
				}
				catch (Exception ex)
				{
					ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Basic_Assets_Check, ScanLog.Stage.s2, "ScanAll", string.Concat(new string[]
					{
						ex.GetType().FullName,
						" ",
						ex.Message,
						"\n",
						ex.StackTrace
					}));
					this.Close();
					this.DeleteOutputFiles();
					this.AllRuleFailed();
					Singleton<ScanOverviewManager>.Instance.SetModulesRunError("CS_Check");
					return;
				}
				bool flag2 = ApiCompatibilityUtils.Instance.HasCompileError();
				if (flag2)
				{
					this.Close();
					this.DeleteOutputFiles();
					this.AllRuleFailed();
					Singleton<ScanOverviewManager>.Instance.SetModulesRunError("CS_Check");
				}
				else
				{
					for (int i = 0; i < this.recordRules_Check.Count; i++)
					{
						RecordRule recordRule = this.recordRules_Check[i];
						ToolsHelper.ShowProgressBar("CS Check", string.Format("Output result:{0}({1}/{2})", recordRule.GetType().Name, i + 1, this.recordRules_Check.Count), 0.5f * (float)(i + 1) / (float)this.recordRules_Check.Count, false);
						recordRule.OutputData(false);
						recordRule.OutputDetailData();
						recordRule.DumpToRuleCSV(ref this.rulesSw);
						recordRule.DumpToThresCSV(ref this.thresSw);
					}
					ScanLog.Instance.LogInfo(Module.CS_Check, ScanLog.Stage.s4, "Done.");
					ToolsHelper.ShowProgressBar("CS Check", "Output result", 0.8f, false);
					RecordManagerFactory.Instance.OutPutScanScr();
					ScanLog.Instance.LogInfo(Module.CS_Check, ScanLog.Stage.s5, "Done.");
					RecordManagerFactory.Instance.Close();
					this.Close();
				}
			}
		}

		// Token: 0x06001249 RID: 4681 RVA: 0x000553E4 File Offset: 0x000535E4
		private void AllRuleFailed()
		{
			for (int i = 0; i < this.recordRules_Check.Count; i++)
			{
				RecordRule recordRule = this.recordRules_Check[i];
				recordRule.CreateNewOutputDetailFile();
			}
		}

		// Token: 0x0600124A RID: 4682 RVA: 0x00055424 File Offset: 0x00053624
		public override void Init()
		{
			this.config = (Singleton<ConfigMgr>.Instance.GetConfig<CSCheckConfig>() as CSCheckConfig);
			this.baseRules_Check.Clear();
			this.ruleAttr = typeof(CSCheckRuleAttr);
			RecordManagerFactory.Instance.Init();
			this.recordRules_Check.Clear();
			this.globalRules_Check.Clear();
		}

		// Token: 0x0600124B RID: 4683 RVA: 0x00055488 File Offset: 0x00053688
		public override void Close()
		{
			ToolsHelper.ShowProgressBar("CS Check", "End.", 0.8f, false);
			Singleton<PerformanceMeasurer>.Instance.CloseAndRecordState();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("CSCheckEnd");
			Singleton<AssemblyManager>.Instance.DeleteTempDirectory();
			ScanLog.Instance.LogInfo(Module.CS_Check, ScanLog.Stage.s6, "Done.");
		}
	}
}
